<template>
    <div class="bigBox">
     <!-- 最外层的大盒子 -->
     <div class="box">
        <!-- 滑动盒子 -->
        <div class="pre-box" ref="preRef">
            <h1>WELCOME</h1>
            <p>JOIN US!</p>
            <div class="img-box">
                <!-- <img :src="flag == ture ? imgList[1]:imgList[0]" alt=""> -->
            </div>
        </div>
        <!-- 注册盒子 -->
        <div class="register-form ">
            <!-- 标题盒子 -->
            <div class="title-box">
                <h1>注册</h1>
            </div>
            <!-- 输入框盒子 -->
            <el-form ref="registerFormRef" :model="registerForm" :rules="rules" label-with="5px" >
                <el-form-item prop="real_name" label="">
                <el-input type="text" placeholder="请输入您的真实姓名" :prefix-icon='User' v-model="registerForm.real_name"  />
                </el-form-item>
                <el-form-item prop="user_name" label="">
                <el-input type="text" placeholder="请输入您的账号" :prefix-icon='User' v-model="registerForm.user_name"  />
                </el-form-item>
                <el-form-item  prop="password" label="">
                <el-input type="password" placeholder="请输入您的密码" :prefix-icon='Lock' v-model="registerForm.password" />
                </el-form-item>
                <el-form-item prop="confirmPassword" label="">
                <el-input type="password" placeholder="请确认您的密码"  :prefix-icon='Lock'  v-model="registerForm.confirmPassword" />
                </el-form-item>
                <el-form-item prop="phone_num" label="">
                <el-input type="text" placeholder="请确认您的手机号"  :prefix-icon='Lock'  v-model="registerForm.phone_num" />
                </el-form-item>
                <el-form-item prop="role">
                    <el-select  v-model="registerForm.role"  placeholder="请选择您的身份">
                        <el-option label="学生" value="学生" />
                        <el-option label="老师" value="老师" />
                    </el-select>
                </el-form-item>
                <!-- <input type="text" placeholder="用户名">
                <input type="password" placeholder="密码">
                <input type="password" placeholder="确认密码"> -->
            </el-form>
            <!-- 按钮盒子 -->
            <div class="btn-box">
                <button @click="registerBtn">注册</button>
                <!-- 绑定点击事件 -->
                <!-- <p @click="mySwitch">已有账号?去登录</p> -->
            </div>
            <div class="btnPstyle">
                <p class="btnP" @click="mySwitch">没有账号?去注册</p>
            </div>
        </div>
        <!-- 登录盒子 -->
        <div class="login-form">
            <!-- 标题盒子 -->
            <div class="title-box">
                <h1>登录</h1>
            </div>
            <!-- 输入框盒子 -->
            <el-form  ref="loginFormRef" :model="loginForm" :rules="rules" label-with="5px" >
                <el-form-item  prop="user_name" label="">
                <el-input type="text" placeholder="用户名" :prefix-icon='User' v-model="loginForm.user_name"/>
                </el-form-item>
                <el-form-item  prop="password" label="">
                <el-input type="password" placeholder="密码" :prefix-icon='Lock' v-model="loginForm.password" />
                </el-form-item>
                <!-- <input type="password" placeholder="密码"> -->
            </el-form>
            <!-- 按钮盒子 -->
            <div class="btn-box">
                <button @click="loginBtn">登录</button>
                <!-- 绑定点击事件 -->
            </div>
            <div class="btnPstyle">
                <p class="btnP" @click="mySwitch">没有账号?去注册</p>
            </div>
        </div>
       </div>
    </div>
</template>

<script setup>
import { reactive, ref,onMounted } from 'vue';
import { Lock,User } from '@element-plus/icons-vue'
import { useLoginPort,useRegisterPort } from '@/api/login.js'
import { useRouter } from 'vue-router'

const preRef = ref('')
// const imgList = ref([require(''),require('')])
let flag = ref(true)
const mySwitch = () =>{
    if(flag.value){
        preRef.value.style.background = '#c9e0ed'
        preRef.value.style.transform = 'translateX(100%)'
    }else{
        preRef.value.style.background = '#eed4dc'
        preRef.value.style.transform = 'translateX(0%)'
    }
    clearForm()
    flag.value = !flag.value
}

const loginForm = reactive({
    user_name:'admin',
    password:'xike614',
})
const registerForm = reactive({
    real_name:'11',
    user_name:'222222',
    password:'222222',
    confirmPassword:'222222',
    phone_num:'11111111111',
    role:'学生'
})

const clearForm = () =>{
    registerForm.real_name = ''
    registerForm.user_name = ''
    registerForm.password = ''
    registerForm.confirmPassword = ''
    registerForm.role = ''
    registerForm.phone_num = ''


    loginForm.user_name = ''
    loginForm.password = ''
}
const loginFormRef = ref('')
const registerFormRef = ref('')
const rules = reactive({
    user_name:[
        {required:true,message:'请输入您的账号',trigger:'blur'},
        // {min:6,max:9,message:'长度应该在6~9个字符之间'}
    ],
    password:[
        {required:true,message:'请输入密码',trigger:'blur'},
        // {min:6,max:9,message:'长度应该在6~9个字符之间'}
    ],
    confirmPassword:[
        {required:true,message:'请再次输入密码',trigger:'blur'},
        // {min:6,max:9,message:'长度应该在6~9个字符之间'}
    ],
    phone_num:[
        {required:true,message:'请输入正确的手机号',trigger:'blur'},
        {min:11,max:11,message:'请输入正确的手机号'}
    ]
})

const registerBtn = async () =>{
    await registerFormRef.value.validate()
if(registerForm.password !== registerForm.confirmPassword){
    ElMessage({
    // showClose: false,
    message: '两次密码不一样,请重新输入！',
    type: 'false'
  })
  return
}
// console.log('111',registerForm);
console.log('registerForm',registerForm);
const res = await useRegisterPort(registerForm)
// console.log(res,'loginres');
// userStores.setToken(res.data.token)
// userStores.setRole(res.data.role)
// if(formModel.value.username === 'admin' && formModel.value.password === '123456')
  ElMessage({
    showClose: true,
    message: '注册成功,请再次登录',
    type: 'success'
  })
  // console.log('LOGIN1');
//   router.push('/')
  // console.log('LOGIN2');
  mySwitch()
}

const router = useRouter()

import { useUserStore } from '@/stores/index'

const uesInfo = useUserStore()

const loginBtn = async () =>{
    await loginFormRef.value.validate()

console.log('111',loginForm);
const{ data }= await useLoginPort(loginForm)
console.log(data.data,'loginres');

uesInfo.userInfo.id = data.data.id
uesInfo.userInfo.role = data.data.role
uesInfo.userInfo.username = data.data.username
// userStores.setToken(res.data.token)
// userStores.setRole(res.data.role)
// if(formModel.value.username === 'admin' && formModel.value.password === '123456')
if(data.code === 500){
    ElMessage({
    message: '登录失败,' + data.msg,
    type: 'error'
  })
  return 
}
  ElMessage({
    showClose: true,
    message: '登录成功',
    type: 'success'
  })
console.log(data.data);
  router.push({
    name:'Subject',
    // query: {
    //         userData: JSON.stringify(res.data) // 将返回的数据转换为字符串并传递给 'Subject' 页面
    //     }
    // params: { loginData: res.data.data } // 将 res 数据作为参数传递给 'Subject' 组件
  })

  // console.log('LOGIN2');
//   mySwitch()
}




onMounted(() =>{
// console.log(uesInfo.userInfo,'uesrInfo111111');

})
</script>

<style scoped>

/* 去除input的轮廓 */
input {
    outline: none;
}
/* 
html,
body {
    height: 100%;
} */

.bigBox{
    /* 溢出隐藏 */
    height:98vh;
    overflow-x: hidden;
    display: flex;
    /* 渐变方向从左到右 */
    background: linear-gradient(to right, rgb(247, 209, 215), rgb(191, 227, 241));
}

span {
    position: absolute;
    z-index: 0;
    bottom: 0;
    border-radius: 50%;
    /* 径向渐变 */
    background: radial-gradient(circle at 72% 28%, #fff 3px, #ff7edf 8%, #5b5b5b, #aad7f9 100%);
    /* 泡泡内阴影 */
    box-shadow: inset 0 0 6px #fff,
        inset 3px 0 6px #eaf5fc,
        inset 2px -2px 10px #efcde6,
        inset 0 0 60px #f9f6de,
        0 0 20px #fff;
    /* 动画 */
    animation: myMove 4s linear infinite;
}


@keyframes myMove {
    0% {
        transform: translateY(0%);
        opacity: 1;
    }

    50% {
        transform: translate(10%, -1000%);
    }

    75% {
        transform: translate(-20%, -1200%);
    }

    99% {
        opacity: .9;
    }

    100% {
        transform: translateY(-1800%) scale(1.5);
        opacity: 0;
    }
}

/* 最外层的大盒子 */
.box {
    width: 1050px;
    height: 600px;
    display: flex;
    /* 相对定位 */
    position: relative;
    z-index: 2;
    margin: auto;
    /* 设置圆角 */
    border-radius: 8px;
    /* 设置边框 */
    border: 1px solid rgba(255, 255, 255, .6);
    /* 设置盒子阴影 */
    box-shadow: 2px 1px 19px rgba(0, 0, 0, .1);
}

/* 滑动的盒子 */
.pre-box {
    /* 宽度为大盒子的一半 */
    width: 50%;
    /* width: var(--width); */
    height: 100%;
    /* 绝对定位 */
    position: absolute;
    /* 距离大盒子左侧为0 */
    left: 0;
    /* 距离大盒子顶部为0 */
    top: 0;
    z-index: 99;
    border-radius: 4px;
    background-color: #edd4dc;
    box-shadow: 2px 1px 19px rgba(0, 0, 0, .1);
    /* 动画过渡，先加速再减速 */
    transition: 0.5s ease-in-out;
}

/* 滑动盒子的标题 */
.pre-box h1 {
    margin-top: 150px;
    text-align: center;
    /* 文字间距 */
    letter-spacing: 5px;
    color: white;
    /* 禁止选中 */
    user-select: none;
    /* 文字阴影 */
    text-shadow: 4px 4px 3px rgba(0, 0, 0, .1);
}

/* 滑动盒子的文字 */
.pre-box p {
    height: 30px;
    line-height: 30px;
    text-align: center;
    margin: 20px 0;
    /* 禁止选中 */
    user-select: none;
    font-weight: bold;
    color: white;
    text-shadow: 4px 4px 3px rgba(0, 0, 0, .1);
}

/* 图片盒子 */
.img-box {
    width: 200px;
    height: 200px;
    margin: 20px auto;
    /* 设置为圆形 */
    border-radius: 50%;
    /* 设置用户禁止选中 */
    user-select: none;
    overflow: hidden;
    box-shadow: 4px 4px 3px rgba(0, 0, 0, .1);
}

/* 图片 */
.img-box img {
    width: 100%;
    transition: 0.5s;
}

/* 登录和注册盒子 */
.login-form,
.register-form {
    flex: 1;
    height: 100%;
}

/* 标题盒子 */
.title-box {
    height: 200px;
    line-height: 200px;

}

/* 标题 */
.title-box h1 {
    text-align: center;
    color: white;
    /* 禁止选中 */
    user-select: none;
    letter-spacing: 5px;
    text-shadow: 4px 4px 3px rgba(0, 0, 0, .1);

}

/* 输入框盒子 */
.el-form {
    display: flex;
    /* 纵向布局 */
    flex-direction: column;
    /* 水平居中 */
    align-items: center;
}
.el-form-item{
    width: 65%;
}

/* 输入框 */
input {
    /* width: 60%; */
    height: 40px;
    margin-bottom: 20px;
    text-indent: 10px;
    border: 1px solid #fff;
    background-color: rgba(255, 255, 255, 0.3);
    border-radius: 120px;
    /* 增加磨砂质感 */
    backdrop-filter: blur(10px);
}

input:focus {
    /* 光标颜色 */
    color: #b0cfe9;

}

/* 聚焦时隐藏文字 */
input:focus::placeholder {
    opacity: 0;
}

/* 按钮盒子 */
.btn-box {
    display: flex;
    justify-content: center;
}

/* 按钮 */
button {
    width: 100px;
    height: 30px;
    margin: 0 7px;
    line-height: 30px;
    border: none;
    border-radius: 4px;
    background-color: #69b3f0;
    color: white;
}

/* 按钮悬停时 */
button:hover {
    /* 鼠标小手 */
    cursor: pointer;
    /* 透明度 */
    opacity: .8;
}


.btnPstyle{
    display: flex;
    /* width:160px; */
    margin:center;
    justify-content: center;

}
/* 按钮文字 */
.btnP{
    /* width:40px; */
    /* width:160px; */
    height: 30px;
    line-height: 30px;
    /* 禁止选中 */
    user-select: none;
    font-size: 14px;
    color: white;

}

.btnP:hover {
    cursor: pointer;
    color:rgb(252, 255, 192);
    /* border-bottom: 1px solid white; */
}
</style>